package com.fm.common.base

import androidx.viewbinding.ViewBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.fm.common.R
import com.fm.common.databinding.ActivityBaseRecyclerBinding
import com.fm.common.entity.BasePageEntity
import com.fm.common.utils.setEmptyView
import com.fm.common.utils.setLinearLayoutManager
import com.fm.common.wediget.EmptyLayout

/**
 *
 * @Author:         ly2
 * @CreateDate:     2020/12/23
 * @Description:    列表activity基类
 *
 */
abstract class BaseRecyclerActivity<B : ViewBinding, D> : BaseBPActivity<B, BasePresenter<*>>() {
    protected var page = 1       //页数
    protected var isHasNext = false  //是否有下一页
    protected var row = 20
    protected var mAdapter: BaseQuickAdapter<D, BaseViewHolder>? = null
    protected var refreshEnable = false
    protected var emptyType = EmptyLayout.EmptyType.DEFAULT

    override fun initData() {
        super.initData()
        request()
    }

    /**
     * 初始化刷新
     * @param enable 刷新控件是否可用
     */
    protected fun initRefresh(enable: Boolean = true) {
        binding.let { bind ->
            bind as ActivityBaseRecyclerBinding
            if (enable) {
                bind.baseSrlContent.let { sw ->
                    sw.isRefreshing = true
                    sw.setColorSchemeColors(*resources.getIntArray(R.array.refresh_color))
                    sw.setOnRefreshListener {
                        page = 1
                        request()
                    }
                }
            } else {
                bind.baseSrlContent.isEnabled = false
            }
        }
    }

    /**
     * 初始化Adapter
     * @param loadMore  列表是否加载更多可用
     * @param space     列表item间分割size
     */
    protected fun initAdapter(loadMore: Boolean = false, space: Int = 0) {
        binding.let { bind ->
            bind as ActivityBaseRecyclerBinding
            mAdapter = getAdapter()
            bind.baseRlvContent.setLinearLayoutManager(mContext, space)
            bind.baseRlvContent.adapter = mAdapter
            mAdapter?.let { adapter ->
                adapter.isAnimationFirstOnly = true
                adapter.animationEnable = true
                adapter.setAnimationWithDefault(BaseQuickAdapter.AnimationType.ScaleIn)
                if (loadMore) {
                    adapter.loadMoreModule.let {
                        it.setOnLoadMoreListener {
                            bind.baseRlvContent.post {
                                if (isHasNext) {
                                    page++
                                    request()
                                } else {
                                    it.loadMoreEnd()
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 解析列表数据
     */
    protected fun parseData(pageBean: BasePageEntity<D>?) {
        (binding as ActivityBaseRecyclerBinding).baseSrlContent.isRefreshing = false
        if (null == pageBean) {
            mAdapter?.setNewInstance(null)
            mAdapter?.setEmptyView(mContext, EmptyLayout.EmptyType.ERROR){
                request()
            }
            return
        }
        if (pageBean.records.isNullOrEmpty()) {
            if (pageBean.current == 1) {
                mAdapter?.setNewInstance(null)
                mAdapter?.setEmptyView(mContext, emptyType)
            } else {
                mAdapter?.loadMoreModule?.loadMoreFail()
            }
        } else {
            if (pageBean.current == 1) {
                mAdapter?.setNewInstance(pageBean.records as MutableList<D>?)
            } else {
                mAdapter?.addData(pageBean.records!!)
                mAdapter?.loadMoreModule?.loadMoreComplete()
            }
        }
        isHasNext = pageBean.hasNext
    }


    /** 请求网络数据  */
    protected open fun request() {
    }

    /**
     * 初始化adapter
     */
    protected abstract fun getAdapter(): BaseQuickAdapter<D, BaseViewHolder>


}